<?php
$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=mysql;charset=utf8';
$usr = 'root';
$pwd = '';
$opt = [
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];

try {
    $db = new pdo($dsn, $usr, $pwd, $opt);
} catch (PDOException $e) {
    die($e->getMessage());
}


function dbv(): false|array {
    global $db;
    $pst = $db->prepare('select @@version,@@port,@@hostname,@@basedir,@@datadir,now()');
    $pst->execute();
    $result = $pst->fetchAll();
    return $result;
}

function tbs($dbname = null): false|array {
    global $db;
    if (is_null($dbname)) {
        $pst = $db->prepare('show tables');
    } else {
        $pst = $db->prepare('show tables from ' . $dbname);
    }
    $pst->execute();
    $result = $pst->fetchAll();
    foreach ($result as $row) $tbs[] = array_shift($row);
    return $tbs;
}

function dbs(): false|array {
    global $db;
    $pst = $db->prepare('show databases');
    $pst->execute();
    $result = $pst->fetchAll();
    foreach ($result as $row) $dbs[] = array_shift($row);
    return $dbs;
}